package servlet;

import bean.Emp;
import util.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/list")
public class list extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sql = "select * from emp e,dept d where e.DeptID=d.DeptID ";
        ResultSet rs = DBUtil.query(sql);
        ArrayList<Emp> list =new ArrayList<>();
        try {
            while (rs.next()) {
                int empid = rs.getInt("empid");
                String empname = rs.getString("empname");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String tel = rs.getString("tel");
                String password = rs.getString("password");
                int deptid = rs.getInt("deptid");
                String deptname = rs.getString("deptname");
                Emp emp = new Emp(empid, empname, sex, age, tel, password, deptid, deptname);
                list.add(emp);
                request.setAttribute("list", list);
                request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
